<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>转换相关 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter05.html" />
    
    
    <link rel="prev" href="./chapter03.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="4"
        data-chapter-title="转换相关"
        data-filepath="chapter04.md"
        data-basepath="."
        data-revision="Sun Feb 09 2020 19:04:23 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        元组的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02/index.html">
            
                
                    <a href="./chapter02/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        字典的基本使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="chapter02/section01.html">
            
                
                    <a href="./chapter02/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        字典的增删改查
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter02/section02.html">
            
                
                    <a href="./chapter02/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        字典的遍历
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        集合的使用
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        转换相关
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        通用方法
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x6267;&#x884C;&#x5B57;&#x7B26;&#x4E32;">&#x6267;&#x884C;&#x5B57;&#x7B26;&#x4E32;</h2>
<p>&#x4F7F;&#x7528;Python&#x5185;&#x7F6E;&#x7684;eval&#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x5B57;&#x7B26;&#x4E32;&#x91CC;&#x7684;Python&#x4EE3;&#x7801;&#x3002;&#x4F7F;&#x7528;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;&#x4E3A;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-python">x = <span class="hljs-string">&apos;1+1&apos;</span>
print(eval(x))  <span class="hljs-comment"># 2</span>
print(type(eval(x)))  <span class="hljs-comment"># &lt;class &apos;int&apos;&gt;</span>

y = <span class="hljs-string">&apos;{&quot;name&quot;:&quot;zhangsan&quot;,&quot;age&quot;:18}&apos;</span>
print(eval(y))
print(type(eval(y)))  <span class="hljs-comment"># &lt;class &apos;dict&apos;&gt;</span>

print(eval(<span class="hljs-string">&apos;1 &gt; 2&apos;</span>))  <span class="hljs-comment"># False</span>

eval(<span class="hljs-string">&apos;input(&quot;&#x8BF7;&#x8F93;&#x5165;&#x60A8;&#x7684;&#x59D3;&#x540D;:&quot;)&apos;</span>)
</code></pre>
<h2 id="&#x8F6C;&#x6362;&#x6210;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;">&#x8F6C;&#x6362;&#x6210;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;</h2>
<p>JSON(JavaScriptObjectNotation, JS&#x5BF9;&#x8C61;&#x7B80;&#x8C31;)&#x662F;&#x4E00;&#x79CD;&#x8F7B;&#x91CF;&#x7EA7;&#x7684;&#x6570;&#x636E;&#x4EA4;&#x6362;&#x683C;&#x5F0F;&#xFF0C;&#x5B83;&#x57FA;&#x4E8E; ECMAScript &#x7684;&#x4E00;&#x4E2A;&#x5B50;&#x96C6;&#xFF0C;&#x91C7;&#x7528;&#x5B8C;&#x5168;&#x72EC;&#x7ACB;&#x4E8E;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x7684;<strong>&#x6587;&#x672C;&#x683C;&#x5F0F;</strong>&#x6765;&#x5B58;&#x50A8;&#x548C;&#x8868;&#x793A;&#x6570;&#x636E;&#x3002;<span style="color:red;">JSON&#x672C;&#x8D28;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;</span></p>
<p>JSON&#x7684;&#x529F;&#x80FD;&#x5F3A;&#x5927;&#xFF0C;&#x4F7F;&#x7528;&#x573A;&#x666F;&#x4E5F;&#x975E;&#x5E38;&#x7684;&#x5E7F;&#xFF0C;&#x76EE;&#x524D;&#x6211;&#x4EEC;&#x53EA;&#x4ECB;&#x7ECD;&#x5982;&#x4F55;&#x4F7F;&#x7528;Python&#x7684;&#x5185;&#x7F6E;JSON&#x6A21;&#x5757;&#xFF0C;&#x5B9E;&#x73B0;&#x5B57;&#x5178;&#x3001;&#x5217;&#x8868;&#x6216;&#x8005;&#x5143;&#x7EC4;&#x4E0E;&#x5B57;&#x7B26;&#x4E32;&#x4E4B;&#x95F4;&#x7684;&#x76F8;&#x4E92;&#x8F6C;&#x6362;&#x3002;</p>
<p>&#x4F7F;&#x7528;json&#x7684;dumps&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x5B57;&#x5178;&#x3001;&#x5217;&#x8868;&#x6216;&#x8005;&#x5143;&#x7EC4;&#x8F6C;&#x6362;&#x6210;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json

person = {<span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&apos;zhangsan&apos;</span>, <span class="hljs-string">&apos;age&apos;</span>: <span class="hljs-number">18</span>}
x = json.dumps(person)
print(x)  <span class="hljs-comment"># {&quot;name&quot;: &quot;zhangsan&quot;, &quot;age&quot;: 18}</span>
print(type(x))  <span class="hljs-comment"># &lt;class &apos;str&apos;&gt;</span>

nums = [<span class="hljs-number">1</span>, <span class="hljs-number">9</span>, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>, <span class="hljs-number">7</span>]
y = json.dumps(nums)
print(y)  <span class="hljs-comment"># [1, 9, 0, 4, 7]</span>
print(type(y)) <span class="hljs-comment"># &lt;class &apos;str&apos;&gt;</span>

words = (<span class="hljs-string">&apos;hello&apos;</span>,<span class="hljs-string">&apos;good&apos;</span>,<span class="hljs-string">&apos;yes&apos;</span>)
z = json.dumps(words)
print(z) <span class="hljs-comment"># [&quot;hello&quot;, &quot;good&quot;, &quot;yes&quot;]</span>
print(type(z)) <span class="hljs-comment"># &lt;class &apos;str&apos;&gt;</span>
</code></pre>
<p>&#x4F7F;&#x7528;json&#x7684;loads&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x683C;&#x5F0F;&#x6B63;&#x786E;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;&#x4E3A;&#x5B57;&#x5178;&#x3001;&#x5217;&#x8868;&#x3002;</p>
<pre><code class="lang-python">x = <span class="hljs-string">&apos;{&quot;name&quot;: &quot;zhangsan&quot;, &quot;age&quot;: 18}&apos;</span>
person = json.loads(x)
print(person)  <span class="hljs-comment"># {&apos;name&apos;: &apos;zhangsan&apos;, &apos;age&apos;: 18}</span>
print(type(person)) <span class="hljs-comment"># &lt;class &apos;dict&apos;&gt;</span>

y = <span class="hljs-string">&apos;[1, 9, 0, 4, 7]&apos;</span>
nums = json.loads(y)
print(nums) <span class="hljs-comment"># [1, 9, 0, 4, 7]</span>
print(type(nums)) <span class="hljs-comment"># &lt;class &apos;list&apos;&gt;</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter03.html" class="navigation navigation-prev " aria-label="Previous page: 集合的使用"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter05.html" class="navigation navigation-next " aria-label="Next page: 通用方法"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
